package com.sun.lg.service.impl;

import com.sun.lg.anno.Autowired;
import com.sun.lg.anno.Service;
import com.sun.lg.anno.Transactional;
import com.sun.lg.dao.AccountDao;
import com.sun.lg.service.TransferService;
import com.sun.lg.pojo.Account;

@Service(value = "transferService")
@Transactional
public class TransferServiceImpl implements TransferService {

    @Autowired
    private AccountDao accountDao;

    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }

    @Override
    public void transfer(String fromCardNo, String toCardNo, int money) throws Exception {
        Account from = accountDao.queryAccountByCardNo(fromCardNo);
        Account to = accountDao.queryAccountByCardNo(toCardNo);
        from.setMoney(from.getMoney() - money);
        to.setMoney(to.getMoney() + money);
        accountDao.updateAccountByCardNo(to);
//        int c = 1 / 0;
        accountDao.updateAccountByCardNo(from);
    }
}
